package com.example.myletcodelearing.stack

import com.example.myletcodelearing.chaintable.TestDulChainNode

/**
 * @author tgw
 * @date 2022/8/2
 * @describe
 */
class StackNode {



    var value:Int=-1
}

fun main(array: Array<String>) {
    println("测试开始栈")
    val test = TestStackNode()
    test.push1(1)
    test.push1(3)

    test.pop1()
    test.pop1()
    test.push1(5)
//    test.printValue()

    val test2 = TestStackNode2()

    test2.push1(1,1)
    test2.push1(3,2)
    test2.push1(4,2)
    test2.pop1(1)
    test2.printValue()
}
class TestStackNode {
    var top1=-1
    var top2=-1
    var nodes=Array<StackNode>(10){ i->StackNode()}

    fun push1(data: Int){
        if (top1>=nodes.size){
            println("越界：$top1")
            return
        }
        top1++
        nodes[top1].value =data
    }

    fun pop1():Int{
        if (top1==-1){
            println("越界：$top1")
            return -1
        }else{
            val value = nodes[top1].value
            top1--
            return value
        }
    }

    fun printValue(){
        var index = top1
        while (index>-1){
            println("值：${nodes[index].value}")
            index--
        }
    }
}


class TestStackNode2 {
    val maxSize = 3
    var top1=-1
    var top2=maxSize
    var nodes=Array<StackNode>(maxSize){ i->StackNode()}

    fun push1(data: Int,top: Int){
        if (top1+1==top2){
            println("越界：$top1")
            return
        }
        if (top == 1){
            top1++
            nodes[top1].value =data
        }else if (top == 2){
            top2--
            nodes[top2].value =data
        }

    }

    fun pop1(top: Int):Int{
        if (top==1){
            if (top1==-1){
                println("越界 栈类型：$top-----指标$top1")
                return -1
            }else{
                val value = nodes[top1].value
                top1--
                return value
            }
        }else if (top==2){
            if (top2==maxSize){
                println("越界 栈类型：$top-----指标$top2")
                return -1
            }else{
                val value = nodes[top2].value
                top2++
                return value
            }
        }else{
            println("越界 栈类型：$top----")
            return -1
        }

    }

    fun printValue(){
        var index = top1
        var index2 = top2
        while (index>-1){
            println("值：${nodes[index].value}")
            index--
        }

        while (index2<maxSize){
            println("值：${nodes[index2].value}")
            index2++
        }
    }
}